package com.itcast.security.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itcast.common.model.dto.request.CheckItemAddReqDTO;
import com.itcast.common.model.dto.request.CheckItemGroupReqDTO;
import com.itcast.common.model.dto.request.CheckItemReqDTO;
import com.itcast.common.model.dto.response.CheckItemGroupResDTO;
import com.itcast.common.model.dto.response.CheckItemResDTO;
import com.itcast.security.mapper.CheckItemMapper;
import com.itcast.security.model.Checkitem;
import com.itcast.security.service.ICheckItemService;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CheckItemServiceImpl extends ServiceImpl<CheckItemMapper, Checkitem> implements ICheckItemService {
    private final TransactionTemplate transactionTemplate;

    public CheckItemServiceImpl(TransactionTemplate transactionTemplate) {
        this.transactionTemplate = transactionTemplate;
    }

    /**
     * 分页查询检查项
     *
     * @param checkItemReqDTO
     * @return
     */
    @Override
    public PageResult<CheckItemResDTO> queryItemList(CheckItemReqDTO checkItemReqDTO) {
        return PageHelperUtils.selectPage(checkItemReqDTO,
                () -> baseMapper.queryItemList(checkItemReqDTO));
    }


    /**
     * 新增体检项
     *
     * @param checkItemAddReqDTO
     * @return
     */
    @Override
    public Checkitem addCheck(CheckItemAddReqDTO checkItemAddReqDTO) {
        String itemName = checkItemAddReqDTO.getItemName();
        Long count = lambdaQuery().eq(Checkitem::getItemName, itemName).count();
        if(count > 0){
            throw new RuntimeException("检查项名称已存在");
        }
        Checkitem checkitem = BeanUtil.toBean(checkItemAddReqDTO, Checkitem.class);
        checkitem.setCreateTime(LocalDateTime.now().toString());

        Integer checkGroupId = checkItemAddReqDTO.getCheckGroupId();
        if(checkGroupId != null){
            checkitem.setGroupId(checkGroupId.toString());
        }
        //插入数据库
        int insert = baseMapper.insert(checkitem);
        if(insert <= 0){
            throw new RuntimeException("添加检查项失败");
        }
        return checkitem;
    }

    /**
     * 根据ID查询
     *
     * @param id
     * @return
     */
    @Override
    public CheckItemResDTO getByIdCheCk(Long id) {
        CheckItemResDTO checkitem =baseMapper.getCheck(id);
        return checkitem;
    }

    /**
     * 启用检查项
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public Checkitem enable(Long id) {
        if(id == null){
            throw new RuntimeException("参数异常");
        }
        Checkitem checkitem = getById(id);
        if(ObjectUtil.isNull(checkitem)){
            throw new RuntimeException("查询数据不存在");
        }
        Integer itemStatus = checkitem.getItemStatus();
        if(itemStatus == 1){
            throw new RuntimeException("检查项已启用");
        }
        checkitem.setItemStatus(1);
        boolean update = updateById(checkitem);
        if(!update){
            throw new RuntimeException("启用检查项失败");
        }
        return checkitem;
    }

    /**
     * 禁用体检项
     *
     * @param id
     * @return
     */
    @Override
    public Checkitem checkDisable(Long id) {
        if(id == null){
            throw new RuntimeException("参数异常");
        }
        Checkitem checkitem = getById(id);
        if(ObjectUtil.isNull(checkitem)){
            throw new RuntimeException("查询数据不存在");
        }
        return null;
    }

    @Override
    public Checkitem removeCheck(Long id) {
        Checkitem checkitem = getById(id);
        if(ObjectUtil.isNull(checkitem)){
            throw new RuntimeException("查询数据不存在");
        }
        Integer itemStatus = checkitem.getItemStatus();
        if(itemStatus == 1){
            throw new RuntimeException("检查项已启用，不能删除");
        }
        boolean flag = removeById(id);
        if(!flag){
            throw new RuntimeException("删除检查项失败");
        }
        return checkitem;
    }


    /**
     * 修改体检项
     *
     * @param id
     * @param checkItemAddReqDTO
     * @return
     */
    @Override
    public Checkitem updateCheckGroup(Long id, CheckItemAddReqDTO checkItemAddReqDTO) {
        Checkitem checkitem = getById(id);
        if(ObjectUtil.isNull(checkitem)){
            throw new RuntimeException("查询数据不存在");
        }
        String itemName = checkItemAddReqDTO.getItemName();
        Long count = lambdaQuery().eq(Checkitem::getItemName, itemName).ne(Checkitem::getId,id).count();

        if(count > 0){
            throw new RuntimeException("检查项名称已存在");
        }
        checkitem = BeanUtil.toBean(checkItemAddReqDTO, Checkitem.class);
        checkitem.setUpdateTime(LocalDateTime.now().toString());
        boolean flag = updateById(checkitem);

        if(!flag){
            throw new RuntimeException("修改检查项失败");
        }
        return checkitem;
    }

    /**
     * 查询启用状态下的检查组和检查项
     *
     * @param checkItemGroupReqDTO
     * @return
     */
    @Override
    public List<CheckItemGroupResDTO> queryCheckItemGroup(CheckItemGroupReqDTO checkItemGroupReqDTO) {
        List<CheckItemGroupResDTO> checkItemGroupResDTOS = baseMapper.queryCheckItemGroup(checkItemGroupReqDTO);
        return checkItemGroupResDTOS;

    }
}
